// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderLists: null,
    index: 0,
    hidden: true,
    flag: true
  },
  // 确认收货
  Ready(event) {

    wx.showModal({
      title: '确认收货',
      success:(res)=>{
        if (res.confirm) {
          let id = event.currentTarget.dataset.id
          let list = this.data.orderLists
          let index = list.findIndex(item => item.orderId == id)
          list[index].type = 4
          this.setData({
            orderLists: list
          })
      
          wx.setStorageSync('goodsOrderList', list)
        } else if (res.cancel) {
          console.log("取消");
        }
        
      }
    })

  

  },
  // 催发货
  open(event) {

    if (this.data.flag) {
      this.setData({
        hidden: false
      });
      var myTimer = setInterval(() => {
        this.setData({
          hidden: true,
        }); //定时器内要跑的代码
        if (this.data.hidden == true) {
          clearInterval(myTimer)
        }
      }, 1000) //定时循环的时长 毫秒 ，1秒=1000毫秒

    }
    this.setData({
      flag: false
    })

    var Timer = setInterval(() => {

      let id = event.currentTarget.dataset.id
      let list = this.data.orderLists
      let index = list.findIndex(item => item.orderId == id)
      list[index].type = 3
      this.setData({
        orderLists: list
      })

      wx.setStorageSync('goodsOrderList', list)
      if (list[index].type = 3) {
        clearInterval(Timer)
      }
    }, 1000) //定时循环的时长 毫秒 ，1秒=1000毫秒

  },
  close() {


    console.log("关闭弹框");
  },
  // 修改下标
  setIndex(event) {
    let index = event.target.dataset.index;
    this.setData({
      index: index
    })
  },

  // 前往支付
  goPay(event) {
    console.log(event.currentTarget.dataset.id);
    let id = event.currentTarget.dataset.id
    let orderLists = wx.getStorageSync('goodsOrderList');
    let payLists = orderLists.filter(item => item.type == 1)
    let payOrder = payLists.filter(item => item.orderId == id)
    wx.setStorageSync('goodsOrderLst', payOrder)
    console.log(payLists);

    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      index:options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let orderLists = wx.getStorageSync('goodsOrderList');
    console.log(orderLists);
    this.setData({
      orderLists: orderLists
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 关闭所有页面，打开到应用内的某个页面
    wx.reLaunch({
      url: '/pages/my/my',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})